package com.zego.wawaji.base.media;

import android.text.TextUtils;

import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.module.ModuleBase;
import com.noble.library.file.FileUtil;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.bean.UpLoadResult;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 媒体文件管理类
 * Created by Su on 2017/4/12.
 */
public class MediaMgr implements ModuleBase {
    // 文件拼接地址
    public static final String URL_HEAD = "http://yanyuan01.oss-cn-beijing.aliyuncs.com/uploads/";

    private ArrayList<String> mediaUrls = new ArrayList<>();  // 维护多文件上传地址
    private ArrayList<String> mediaFullUrls = new ArrayList<>();  // 维护多文件全称上传地址

    @Override
    public void init() {
    }

    @Override
    public void release() {
    }

    // ================================== 老缘分吧 =====================================

    /**
     * 所有 文件,图片,语音上传等
     *
     * @param uploadType 上传类型
     * @param url        文件地址
     */
    public void sendHttpFile(String uploadType, String url, RequestComplete complete) {
        sendHttpFile(uploadType, url, null, complete);
    }

    /**
     * 上传文件
     *
     * @param uploadType 头像avatar，大头像avatar_big, 相册photo, 语音voice, 自定义表情face
     *                   头像认证videochat，视频video
     * @param url        文件地址
     * @param url2       视频缩略图地址
     */
    private void sendHttpFile(String uploadType, final String url, String url2, final RequestComplete complete) {
        if (TextUtils.isEmpty(url)) return;
        File file = new File(url);
        if (!file.exists()) return;

        Map<String, File> uploadFile = new HashMap<>();
        uploadFile.put("file", file);

        Map<String, Object> postParams = new HashMap<>();
        postParams.put("use_type", uploadType);

        ModuleMgr.getHttpMgr().uploadFile(UrlParam.uploadFile, postParams, uploadFile, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 多文件上传
     *
     * @param uploadType 上传类型
     * @param index      上传指定位置文件
     * @param complete   完成回调
     * @param url        文件地址
     */
    public void sendHttpMultiFiles(final String uploadType, int index, final OnMultiFilesUploadComplete complete, final String... url) {
        if (mediaUrls == null) mediaUrls = new ArrayList<>();
        if (mediaFullUrls == null) mediaFullUrls = new ArrayList<>();
        if (index >= url.length) {
            complete.onUploadComplete(mediaUrls);
            if (complete instanceof OnMFUFullComplete) {
                ((OnMFUFullComplete) complete).onFullComplete(mediaFullUrls);
            }
            mediaUrls.clear();
            mediaFullUrls.clear();
            return;
        }

        final int tempIndex = index + 1;
        if (FileUtil.isExist(url[index])) {  // 当文件存在时，才进行上传任务
            sendHttpFile(uploadType, url[index], response -> {
                if (response.isOk()) {
                    UpLoadResult upLoadResult = (UpLoadResult) response.getBaseData();
                    String pic = upLoadResult.getFile_http_path();
                    if (!TextUtils.isEmpty(pic)) {
//                            String post_url = ModuleMgr.getCenterMgr().getInterceptUrl(pic);
                        mediaFullUrls.add(pic);
//                            mediaUrls.add(post_url);
                    }
                    sendHttpMultiFiles(uploadType, tempIndex, complete, url);
                } else {
                    sendHttpMultiFiles(uploadType, tempIndex, complete, url);
                }
            });
        } else {
            sendHttpMultiFiles(uploadType, tempIndex, complete, url);  // 跳过不存在文件继续上传
        }
    }

    /**
     * 多文件上传结果回调
     */
    public interface OnMultiFilesUploadComplete {
        /**
         * @param mediaUrls 上传成功文件地址
         */
        void onUploadComplete(ArrayList<String> mediaUrls);
    }

    /**
     * 多文件上传结果回调 完整路径
     */
    public interface OnMFUFullComplete extends OnMultiFilesUploadComplete {
        void onFullComplete(ArrayList<String> mediaFullUrls);
    }
}
